/************************************************************************************/
/*  Program name CAPAPIMP_INV_PAYMENT.sql                                           */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER AP_SSE_NOTIFY_EMPLOYEE DISABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARDI1 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARU1 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARU2 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_INV_PAYMENT';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_INV_PAYMENT;

	lv_Next_Seq_ID	NUMBER;
	lv_COA_ID		NUMBER;
BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_INVOICE_PAYMENTS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_INV_PAYMENT_MAP VALUES
			(
			C1.INVOICE_PAYMENT_ID,
			lv_Next_Seq_ID
			);

			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(C1.SOB_SHORT_NAME);

			INSERT INTO AP_INVOICE_PAYMENTS_ALL
			VALUES
			(
			C1.ACCOUNTING_EVENT_ID,										--Need update
			C1.ACCOUNTING_DATE,
			C1.ACCRUAL_POSTED_FLAG,
			C1.AMOUNT,
			C1.CASH_POSTED_FLAG,
			CAPBRIMP_MAPPING.GetCheckIDMapping(C1.CHECK_ID),			--CHECK_ID,
			CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.INVOICE_ID),		--INVOICE_ID,
			lv_Next_Seq_ID,												--INVOICE_PAYMENT_ID,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),			--LAST_UPDATED_BY,
			C1.LAST_UPDATE_DATE,
			C1.PAYMENT_NUM,
			C1.PERIOD_NAME,
			C1.POSTED_FLAG,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),				--SET_OF_BOOKS_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ACCTS_PAY_CCID_D),	--ACCTS_PAY_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ASSET_CCID_D),		--ASSET_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),				--CREATED_BY,
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.BANK_ACCOUNT_NUM,
			C1.BANK_ACCOUNT_TYPE,
			C1.BANK_NUM,
			C1.DISCOUNT_LOST,
			C1.DISCOUNT_TAKEN,
			C1.EXCHANGE_DATE,
			C1.EXCHANGE_RATE,
			C1.EXCHANGE_RATE_TYPE,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.GAIN_CCID_D),		--GAIN_CODE_COMBINATION_ID,
			C1.INVOICE_BASE_AMOUNT,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.LOSS_CCID_D),		--LOSS_CODE_COMBINATION_ID,
			C1.PAYMENT_BASE_AMOUNT,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE_CATEGORY,
			C1.CASH_JE_BATCH_ID,										--????
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.FUTURE_PAY_CCID_D),	--FUTURE_PAY_CODE_COMBINATION_ID,
			C1.FUTURE_PAY_POSTED_FLAG,
			C1.JE_BATCH_ID,												--????
			C1.ELECTRONIC_TRANSFER_ID,
			C1.ASSETS_ADDITION_FLAG,
			C1.INVOICE_PAYMENT_TYPE,
			C1.OTHER_INVOICE_ID,										--????
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),						--ORG_ID,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			CAPBRIMP_MAPPING.GetBankACMapping(C1.EXTERNAL_BANK_ACCOUNT_ID),	--EXTERNAL_BANK_ACCOUNT_ID,
			C1.MRC_EXCHANGE_DATE,
			C1.MRC_EXCHANGE_RATE,
			C1.MRC_EXCHANGE_RATE_TYPE,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.MRC_GAIN_CCID_D),	--MRC_GAIN_CODE_COMBINATION_ID,
			C1.MRC_INVOICE_BASE_AMOUNT,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.MRC_LOSS_CCID_D),	--MRC_LOSS_CODE_COMBINATION_ID,
			C1.MRC_PAYMENT_BASE_AMOUNT,
			C1.REVERSAL_FLAG,
			C1.REVERSAL_INV_PMT_ID,	--After
			C1.IBAN_NUMBER
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		UPDATE
			AP_INVOICE_PAYMENTS_ALL
		SET
			REVERSAL_INV_PMT_ID	=	CAPBRIMP_MAPPING.GetInvPaymentIDMapping(REVERSAL_INV_PMT_ID)
		WHERE	INVOICE_PAYMENT_ID	IN
			(
				SELECT	INVOICE_PAYMENT_ID_N
				FROM	CRST_AP_INV_PAYMENT_MAP
			)
		AND		REVERSAL_INV_PMT_ID		IS	NOT NULL;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER AP_SSE_NOTIFY_EMPLOYEE ENABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARDI1 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARU1 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARU2 ENABLE;
